﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Imageo.bintje;
using System.Net;
using log4net;
using System.Threading;

namespace Imageo.ui
{
    public partial class UserSignUp : System.Web.UI.Page
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSignup_click(object sender, EventArgs e)
        {
            if (chkterms.Checked)
            {
                // TODO: A proper membership provider here would be fine
                try
                {
                    ServiceAPI service = new ServiceAPI();

                    registerRequest req = new registerRequest();
                    req.fullName = txtNickname.Text.Trim();
                    req.loginName = txtUsername.Text.Trim();

                    if (log.IsErrorEnabled) log.Debug("Calling: service.registerUser(req)");
                    registerResponse response = ServiceHelper.HandleBintjeErrors(this, service.registerUser(req));
                    
                    // If we had no errors the user has been created and we are logged in at the same time 
                    Session["BintjeSession"] = response.session;

                    Response.Redirect("~/UI/Home.aspx");
                }
                catch (ThreadAbortException) { }
                catch (Exception exc)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error(exc.Source + " - " + exc.Message);
                    }

                    Session["BintjeSession"] = null;
                    lblSignupMsg.Text = exc.Message;
                    Response.Redirect("~/UI/ErrorGeneric.aspx");
                }
            }
            else
            {
                lblSignupMsg.Text = "You must accept \"terms of service\" before signup";
            }
        }
}
}
